package com.frank.reader.controller;

import com.frank.reader.mapper.MemberReadStateMapper;
import com.frank.reader.pojo.Evaluation;
import com.frank.reader.pojo.Member;
import com.frank.reader.pojo.MemberReadState;
import com.frank.reader.service.MemberService;
import com.frank.reader.service.exception.BussinessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 与会员相关业务，如登录、注册等
 */
@Controller
public class MemberController {
    @Resource
    private MemberService memberService;


    @GetMapping("/register.html")
    public ModelAndView showRegister() {
        return new ModelAndView("register");
    }

    @PostMapping("/register")
    @ResponseBody
    public Map register(String vc, String username, String password, String nickname, HttpServletRequest request) {
        String kaptchaVerifyCode = (String) request.getSession().getAttribute("kaptchaVerifyCode");
        Map<String, String> result = new HashMap<>();
        if (vc == null || !vc.equalsIgnoreCase(kaptchaVerifyCode)) {
            result.put("code", "VO01");
            result.put("msg", "验证码错误");
        } else {
            try {
                memberService.createMember(username, password, nickname);
                result.put("code", "0");
                result.put("msg", "success");
            } catch (BussinessException ex) {
                ex.printStackTrace();
                result.put("code", ex.getCode());
                result.put("msg", ex.getMsg());
            }
        }
        return result;
    }

    @GetMapping("/login.html")
    public ModelAndView showLogin() {
        return new ModelAndView("login");
    }


    @PostMapping("/check_login")
    @ResponseBody
    public Map checkLogin(String username, String password, String vc, HttpServletRequest request) {
        HashMap<String, String> result = new HashMap<>();
        // 先校验验证码
        String kaptchaVerifyCode = (String) request.getSession().getAttribute("kaptchaVerifyCode");
        if (vc == null || !vc.equalsIgnoreCase(kaptchaVerifyCode)) {
            result.put("code", "V01");
            result.put("msg", "验证码不正确！");
        } else {
            try {
                Member member = memberService.checkMember(username, password);
                // 登录成功后将用户信息放到Session中
                request.getSession().setAttribute("loginMember", member);
                result.put("code", "0");
                result.put("msg", "success");
            } catch (BussinessException ex) {
                ex.printStackTrace();
                result.put("code", ex.getCode());
                result.put("msg", ex.getMsg());
            }
        }
        return result;
    }

    /**
     * 更新 想看/看过阅读状态
     *
     * @param memberId  会员编号
     * @param bookId    图书编号
     * @param readState 阅读状态
     * @return 处理结果
     */
    @PostMapping("/update_read_state")
    @ResponseBody
    public Map updateReadState(Long memberId, Long bookId, Integer readState) {
        HashMap<String, String> result = new HashMap<>();
        try {
            memberService.updateMemberReadState(memberId, bookId, readState);
            result.put("code", "0");
            result.put("msg", "success");
        } catch (BussinessException ex) {
            ex.printStackTrace();
            result.put("code", ex.getCode());
            result.put("msg", ex.getMsg());
        }
        return result;
    }

    @PostMapping("/evaluate")
    @ResponseBody
    public Map evaluation(Long memberId, Long bookId, Integer score, String content) {
        Map<String, Object> result = new HashMap<>();
        try {
            Evaluation evaluation = memberService.evaluation(memberId, bookId, score, content);
            result.put("code", "0");
            result.put("msg", "success");
            result.put("evaluation", evaluation);
        } catch (BussinessException ex) {
            ex.printStackTrace();
            result.put("code", ex.getCode());
            result.put("msg", ex.getMsg());
        }
        return result;
    }

    @PostMapping("/enjoy")
    @ResponseBody
    public Map enjoy(Long evaluationId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Evaluation evaluation = memberService.enjoy(evaluationId);
            result.put("code", "0");
            result.put("msg", "success");
            result.put("evaluation", evaluation);
        } catch (BussinessException ex) {
            ex.printStackTrace();
            result.put("code", ex.getCode());
            result.put("msg", ex.getMsg());
        }
        return result;
    }
}
